package com.mihuo.config;

import com.mihuo.config.filter.LogFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

    // 将LogFilter声明为Spring Bean（可选，也可在registration中直接new）
    @Bean
    public LogFilter logFilter() {
        return new LogFilter();
    }

    // 注册过滤器并设置路径和顺序
    @Bean
    public FilterRegistrationBean<LogFilter> loggingFilterRegistration(LogFilter logFilter) {
        FilterRegistrationBean<LogFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(logFilter);
        registrationBean.addUrlPatterns("*"); // 设置要拦截的URL模式
        registrationBean.setOrder(1); // 明确设置执行顺序，数字越小优先级越高
        // 还可以设置过滤器名称等
        registrationBean.setName("logFilter");
        return registrationBean;
    }
}